package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BalanceRecordDetailBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(BalanceRecordDetailBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String destinationId = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Destination_Id");
            String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/DestinationAttr");
            String queryFlag = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Query_Flag");
            String billingCycleId = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Billing_Cycle");

            if (queryFlag == null) {
                queryFlag = "0";
            } else if (queryFlag.length() == 0) {
                queryFlag = "0";
            }

            JSONObject operAttrStruct = new JSONObject();
            operAttrStruct.put("operServiceId", "-1");
            operAttrStruct.put("lanId", "8420100");
            operAttrStruct.put("operPost", "0");
            operAttrStruct.put("operOrgId", "0");
            operAttrStruct.put("staffId", "0");
            operAttrStruct.put("operTime", "0");

            JSONObject svcObjectStruct = new JSONObject();
            svcObjectStruct.put("objValue", destinationId);
            svcObjectStruct.put("objAttr", destinationAttr);
            svcObjectStruct.put("objType", "3");
            svcObjectStruct.put("dataArea", queryFlag);


            //构建请求条件
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("operAttrStruct", operAttrStruct);
            jsonObject.put("svcObjectStruct", svcObjectStruct);
            jsonObject.put("billingCycleId", billingCycleId);
            StringBuilder msgBodyStr = new StringBuilder();

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            msgBodyStr.append(jsonObject.toJSONString());
            _log.info("request body is {}", msgBodyStr.toString());
            _log.info("webserviceUrl is {}", webserviceUrl);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), webserviceUrl);
            _log.info("response is {}", respStr);

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            //解析json 拼接报文
            JSONObject json = JSON.parseObject(respStr);
            StringBuffer buffer = new StringBuffer();
            jsonToXmlStr4(xmlMessage, json, buffer);


            bizResult.setHttpBody(buffer.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info("", e);
        }
        return bizResult;
    }

    public static void jsonToXmlStr4(XmlMessage xmlMessage, JSONObject json, StringBuffer buffer) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        buffer.append("<SOAP-ENV:Header/>");
        buffer.append("<SOAP-ENV:Body>");
        buffer.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
        buffer.append("<REPTAG>" + xmlMessage.getREPTAG() + "</REPTAG>");
        buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
        buffer.append("<SIG>" + xmlMessage.getSIG() + "</SIG>");
        buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
        buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
        buffer.append("<DATA><![CDATA[<Service_Information>");
        buffer.append("<Result>" + (json.get("resultCode") == null ? "" : json.get("resultCode")) + "</Result>");
        buffer.append("<Para_Field_Result>" + (json.get("resultMsg") == null ? "" : json.get("resultMsg")) + "</Para_Field_Result>");

        String entryListStr = String.valueOf(json.get("balanceChangeList"));
        JSONArray jsonArray = JSONArray.parseArray(entryListStr);
        if (!entryListStr.equals("null")) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject entry = (JSONObject) jsonArray.get(i);
                buffer.append("<BalanceRecordDetailInfo>");
                buffer.append("<Billing_Cycle>" + (entry.get("billingCycleId") == null ? "" : entry.get("billingCycleId")) + "</Billing_Cycle>");
                buffer.append("<StateDate>" + (entry.get("stateDate") == null ? "" : entry.get("stateDate")) + "</StateDate>");
                buffer.append("<BalanceChangeType>" + (entry.get("balanceChangeType") == null ? "" : entry.get("balanceChangeType")) + "</BalanceChangeType>");
                buffer.append("<BalanceIn>" + (entry.get("balanceIn") == null ? "" : entry.get("balanceIn")) + "</BalanceIn>");
                buffer.append("<BalanceOut>" + (entry.get("balanceOut") == null ? "" : entry.get("balanceOut")) + "</BalanceOut>");
                buffer.append("<BalanceEnd>" + (entry.get("balanceEnd") == null ? "" : entry.get("balanceEnd")) + "</BalanceEnd>");
                buffer.append("<BalanceTypeFlag>" + (entry.get("balanceTypeFlag") == null ? "" : entry.get("balanceTypeFlag")) + "</BalanceTypeFlag>");
                buffer.append("<AccNbrDetai>" + (entry.get("accNbrDetail") == null ? "" : entry.get("accNbrDetail")) + "</AccNbrDetai>");
                buffer.append("<Payment_Serial_Nbr>" + (entry.get("serviceId") == null ? "" : entry.get("serviceId")) + "</Payment_Serial_Nbr>");
                buffer.append("<Balance_Type>" + (entry.get("balanceTypeFlag") == null ? "" : entry.get("balanceTypeFlag")) + "</Balance_Type>");
                buffer.append("<Acct_Balance_Id></Acct_Balance_Id>");
                buffer.append("<Balance_ItemType_Detail>" + (entry.get("balanceTypeName") == null ? "" : entry.get("balanceTypeName")) + "</Balance_ItemType_Detail>");
                buffer.append("</BalanceRecordDetailInfo>");
            }
        }

        buffer.append("</Service_Information>]]></DATA>");
        buffer.append("</ns3:SgwQueryAck>");
        buffer.append("</SOAP-ENV:Body>");
        buffer.append("</SOAP-ENV:Envelope>");
    }


}
